<template>
  <div>
    <el-form ref="form" :model="form" inline>
      <el-form-item label="开始时间" style="font-weight:bold;">
        <el-date-picker v-model="form.startTime" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd" style="width:180px;" clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" style="font-weight:bold;">
        <el-date-picker v-model="form.endTime" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd" style="width:180px;" clearable>
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="创建人" style="font-weight:bold;">
        <el-input v-model="form.operator" placeholder="请输入" style="width:150px;" clearable></el-input>
      </el-form-item> -->
      <el-button size="small" class="search_button" type="primary" icon="el-icon-search" @click="getForm">查询</el-button>
      <el-button size="small" class="reset_button" icon="el-icon-refresh" @click="reset">重置</el-button>
    </el-form>
    <el-button size="small" type="success" plain icon="el-icon-download" @click="exportList">导出打卡信息</el-button>
    <el-divider></el-divider>


    <el-table :data="tableData" border size="medium" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="operator" label="创建人" align="center">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center">
      </el-table-column>
      <el-table-column prop="startTime" label="开始时间" align="center">
      </el-table-column>
      <el-table-column prop="endTime" label="结束时间" align="center">
      </el-table-column>
      <el-table-column label="打卡详情" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-search" @click="showDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>

    <el-drawer title="查看打卡详情" :visible.sync="showTaskDetail" direction="ltr" size="70%">
      <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="正常打卡" name="first">
          <el-table :data="tableDataStatusOne" border size="medium">
            <el-table-column prop="name" label="姓名" align="center">
            </el-table-column>
            <el-table-column prop="num" label="学号" align="center">
            </el-table-column>
            <el-table-column prop="grade" label="年级" align="center"></el-table-column>
            <el-table-column prop="collegeName" label="院系" align="center"></el-table-column>

            <el-table-column prop="userType" label="学生类型" align="center">
              <template slot-scope="scope">
                <span v-if="(scope.row.userType == 0)">本科生</span>
                <span v-if="(scope.row.userType == 1)">硕士生</span>
                <span v-if="(scope.row.userType == 2)">博士生</span>
                <span v-if="(scope.row.userType == 3)">留学生</span>
                <span v-if="(scope.row.userType == 4)">交换生</span>
                <span v-if="(scope.row.userType == 5)">专科生</span>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="记录时间" align="center">
            </el-table-column>
          </el-table>
          <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="totalOne"
            :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChangeOne"
            @size-change="handleSizeChangeOne"></el-pagination>
        </el-tab-pane>
        <el-tab-pane label="超时打卡" name="second">
          <el-table :data="tableDataStatusTwo" border size="medium">
            <el-table-column prop="name" label="姓名" align="center">
            </el-table-column>
            <el-table-column prop="num" label="学号" align="center">
            </el-table-column>
            <el-table-column prop="grade" label="年级" align="center"></el-table-column>
            <el-table-column prop="collegeName" label="院系" align="center"></el-table-column>

            <el-table-column prop="userType" label="学生类型" align="center">
              <template slot-scope="scope">
                <span v-if="(scope.row.userType == 0)">本科生</span>
                <span v-if="(scope.row.userType == 1)">硕士生</span>
                <span v-if="(scope.row.userType == 2)">博士生</span>
                <span v-if="(scope.row.userType == 3)">留学生</span>
                <span v-if="(scope.row.userType == 4)">交换生</span>
                <span v-if="(scope.row.userType == 5)">专科生</span>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="记录时间" align="center">
            </el-table-column>
          </el-table>
          <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="totalTwo"
            :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChangeTwo"
            @size-change="handleSizeChangeTwo"></el-pagination>
        </el-tab-pane>
        <el-tab-pane label="未打卡" name="third">
          <el-table :data="tableDataStatusThree" border size="medium">
            <el-table-column prop="name" label="姓名" align="center">
            </el-table-column>
            <el-table-column prop="num" label="学号" align="center">
            </el-table-column>
            <el-table-column prop="grade" label="年级" align="center"></el-table-column>
            <el-table-column prop="collegeName" label="院系" align="center"></el-table-column>

            <el-table-column prop="userType" label="学生类型" align="center">
              <template slot-scope="scope">
                <span v-if="(scope.row.userType == 0)">本科生</span>
                <span v-if="(scope.row.userType == 1)">硕士生</span>
                <span v-if="(scope.row.userType == 2)">博士生</span>
                <span v-if="(scope.row.userType == 3)">留学生</span>
                <span v-if="(scope.row.userType == 4)">交换生</span>
                <span v-if="(scope.row.userType == 5)">专科生</span>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="记录时间" align="center">
            </el-table-column>
          </el-table>
          <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper"
            :total="totalThree" :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChangeThree"
            @size-change="handleSizeChangeThree"></el-pagination>
        </el-tab-pane>
      </el-tabs>
    </el-drawer>
  </div>
</template>

<script>
import { getForm, getDetail, exportList } from '_api/DormManage/CheckRoom/index.js'
export default {
  data() {
    return {
      showTaskDetail: false,
      form: {
        current: 0,
        pageSize: 10,
        startTime: "",
        endTime: "",

      },
      tableData: [],
      total: 0,
      timeNow: "",
      activeName: 'first',
      tableDataStatusOne: [],
      totalOne: 0,
      tableDataStatusTwo: [],
      totalTwo: 0,
      tableDataStatusThree: [],
      totalThree: 0,
      formDetail: {
        special: "",
        state: "",
        current: 0,
        pageSize: 10,
      },
      multipleSelection: []
    }
  },

  created() {
    this.getForm()
  },
  methods: {
    // 导出打卡信息
    exportList() {
      if (this.multipleSelection.length == 0) {
        this.$message.warning('请先勾选需要导出的数据！')
        return false
      }
      exportList(this.multipleSelection).then(res => {
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '打卡记录_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('下载成功！')
      })
    },
    handleSelectionChange(val) {
      val.forEach(item => {
        if (!this.multipleSelection.includes(item.special)) {
          this.multipleSelection.push(item.special)
        }
      })
      if (val.length == 0) {
        this.multipleSelection = []
      }
      console.log(this.multipleSelection);
    },
    handleClick(tab) {
      console.log(tab.name)
      if (tab.name == 'first') {
        this.formDetail.state = 0
        this.getDetailOne()
      }
      if (tab.name == 'second') {
        this.formDetail.state = 1
        this.getDetailTwo()
      }
      if (tab.name == 'third') {
        this.formDetail.state = 2
        this.getDetailThree()
      }
    },
    showDetail(e) {
      this.showTaskDetail = true
      this.activeName = 'first'
      this.formDetail.special = e.special
      this.formDetail.state = 0
      this.getDetailOne()
    },
    getDetailOne() {
      getDetail(this.formDetail).then(res => {
        if (res.data.success == true) {
          this.tableDataStatusOne = res.data.data.list
          this.totalOne = res.data.data.total
        } else {
          this.tableDataStatusOne = res.data.data
          this.totalOne = ""
          this.$message.info(res.data.message)
        }
      })
    },
    getDetailTwo() {
      getDetail(this.formDetail).then(res => {
        if (res.data.success == true) {
          this.tableDataStatusTwo = res.data.data.list
          this.totalTwo = res.data.data.total
        } else {
          this.tableDataStatusTwo = res.data.data
          this.totalTwo = ""
          this.$message.info(res.data.message)
        }
      })
    },
    getDetailThree() {
      getDetail(this.formDetail).then(res => {
        if (res.data.success == true) {
          this.tableDataStatusThree = res.data.data.list
          this.totalThree = res.data.data.total
        } else {
          this.tableDataStatusThree = res.data.data
          this.totalThree = ""
          this.$message.info(res.data.message)
        }
      })
    },

    //重置
    reset() {
      this.form = {
        current: 0,
        pageSize: 10,
        startTime: "",
        endTime: "",
      },
        this.getForm()
    },
    //当前页
    handleCurrentChange(e) {
      this.form.current = (e - 1) * 10
      this.getForm()
    },
    handleSizeChange(e) {
      this.form.pageSize = e
      this.getForm()
    },
    //当前页
    handleCurrentChangeOne(e) {
      this.formDetail.current = (e - 1) * 10
      this.getDetailOne()
    },
    handleSizeChangeOne(e) {
      this.formDetail.pageSize = e
      this.getDetailOne()
    },
    //当前页
    handleCurrentChangeTwo(e) {
      this.formDetail.current = (e - 1) * 10
      this.getDetailTwo()
    },
    handleSizeChangeTwo(e) {
      this.formDetail.pageSize = e
      this.getDetailTwo()
    },
    //当前页
    handleCurrentChangeThree(e) {
      this.formDetail.current = (e - 1) * 10
      this.getDetailThree()
    },
    handleSizeChangeThree(e) {
      this.formDetail.pageSize = e
      this.getDetailThree()
    },
    //获取当前时间
    getCurrentTime() {
      var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
      this.timeNow = _this.gettime
    },

    //查询列表信息
    getForm() {
      getForm(this.form).then(res => {
        if (res.data.success == true) {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.tableData = res.data.data
          this.total = ""
          this.$message.info(res.data.message)
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.pagination {
  float: right;
  margin-top: 10px;
}
</style>